package com.example.zhu95.mytimber.listener;

import android.text.TextUtils;
import android.view.GestureDetector;
import android.view.MotionEvent;

import com.example.zhu95.mytimber.MyApplication;
import com.example.zhu95.mytimber.activity.BaseActivity;

import static com.example.zhu95.mytimber.ConstantValue.MusicState_Playing;
import static com.example.zhu95.mytimber.ConstantValue.PlayMusicCmd_Pause;
import static com.example.zhu95.mytimber.ConstantValue.PlayMusicCmd_Play;

/**
 * @author by zhu95 on 2018/3/22.
 */

public class OnMusicGestureListener extends GestureDetector.SimpleOnGestureListener {

    /*
     *   最小距离
     */
    private static final int FLING_MIN_DISTANCE = 50;
    /*
     *  最小速度
     */
    private static final int FLING_MIN_VELOCITY = 0;
    private boolean enableDoubleClick = false;
    private boolean enableFling = false;

    public void enableDoubleClick(boolean enableDoubleClick) {
        this.enableDoubleClick = enableDoubleClick;
    }

    public void enableFling(boolean enableFling) {
        this.enableFling = enableFling;
    }

    //双击事件
    @Override
    public boolean onDoubleTap(MotionEvent e) {
        if (enableDoubleClick) {
            if (TextUtils.equals(BaseActivity.state, MusicState_Playing)) {
                MyApplication.sendPlayMusicCmdBroadcast(BaseActivity.songid, "", PlayMusicCmd_Pause);
            } else {
                MyApplication.sendPlayMusicCmdBroadcast(BaseActivity.songid, "", PlayMusicCmd_Play, BaseActivity.currentPosition);
            }
        }
        return super.onDoubleTap(e);
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        if (enableFling) {
            float x = e1.getX() - e2.getX();
            float x2 = e2.getX() - e1.getX();
            if (x > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
                new OnPlayNextSongClickListener().onClick(null);
            } else if (x2 > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
                new OnPlayNextSongClickListener().onClick(null);
            }
        }
        return super.onFling(e1, e2, velocityX, velocityY);
    }
}
